using System.Threading.Tasks;
using JT.WEB.Areas.Identity.Data;
using JT.WEB.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.EntityFrameworkCore;

namespace JTWEB3.Controllers
{
    [Authorize(Roles = "Administrators")]
    public class UsersController : Controller
    {
        private readonly UserManager<JTWEB3User> _userManager;

        // GET: Users
        public UsersController(UserManager<JTWEB3User> userManager)
        {
            _userManager = userManager;
        }
        public async Task<ActionResult> Index()
        {
            System.Collections.Generic.List<JTWEB3User> users = await _userManager.Users.ToListAsync();
            //foreach (var s in users)
            //{
            //    var userviewmodel = new Users
            //    {
            //        Id = user.Id;
            //    }
            //}

            return View(users);
        }

        // GET: Users/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        // GET: Users/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Users/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create(Users mUsers)
        {
            try
            {
                // TODO: Add insert logic here
                JTWEB3User user = new JTWEB3User
                {
                    UserName = mUsers.UserName
                };
                IdentityResult result = await _userManager.CreateAsync(user, mUsers.Password);
                if (result.Succeeded)
                {
                    return RedirectToAction(nameof(Index));
                }
                ModelState.AddModelError(string.Empty, "添加用户失败");
                return View();
            }
            catch
            {
                ModelState.AddModelError(string.Empty, "添加用户失败");
                return View();
            }
        }
        //verifyUser
        [AcceptVerbs("Get", "Post")]
        public async Task<JsonResult> VerifyUserAsync(string Email)
        {
            JTWEB3User m = await _userManager.FindByEmailAsync(Email).ConfigureAwait(false);
            if (m is null)
            {
                return Json("验证通过");
            }
            return Json("验证失败");
        }

        // GET: Users/Edit/5
        public async Task<ActionResult> Edit(string userId)
        {
            JTWEB3User user = await _userManager.Users.SingleOrDefaultAsync(u => u.Id == userId);
            return View(user);
        }

        // POST: Users/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Edit(
            [Bind(
                nameof(JTWEB3User.NickName),
                nameof(JTWEB3User.Id)
                )]
            JTWEB3User user)
        {
            if (user is null)
            {
                return NotFound();
            }

            if (!ModelState.IsValid)
            {
                return View(user);
            }
            try
            {
                // TODO: Add update logic here
                JTWEB3User dbUser = await _userManager.Users.SingleOrDefaultAsync(u => u.Id == user.Id);
                dbUser.NickName = user.NickName;
                await _userManager.UpdateAsync(dbUser);
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }

        //GET: Users/Delete/5
        //[HttpGet]
        //public ActionResult Delete(JTWEB3User user)
        //{

        //    return View(user);
        //}

        // POST: Users/Delete/5
        [HttpPost]
        //[ValidateAntiForgeryToken]
        public async Task<ActionResult> Delete(string Id)
        {

            // TODO: Add delete logic here
            if (ModelState.IsValid && Id != null)
            {
                JTWEB3User user = await _userManager.FindByIdAsync(Id);
                if (user != null)
                {
                    IdentityResult result = await _userManager.DeleteAsync(user);
                    if (result.Succeeded)
                    {
                        return RedirectToAction(nameof(Index));
                    }
                    else
                    {
                        foreach (IdentityError item in result.Errors)
                        {
                            ModelState.AddModelError(string.Empty, item.Description);
                        }
                    }
                }

            }

            ModelState.AddModelError(string.Empty, "删除失败");
            return View(nameof(Index), await _userManager.Users.ToListAsync());
        }


    }
}